package org.ymh.unix_file_system.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Repository;
import org.ymh.unix_file_system.domain.GlobalVariable;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@Repository
public class FoundationPropertyOperate
{
    @Autowired
    public GlobalVariable globalVariable;
    public void modifyKey(String key,String value)
    {
        Properties properties = new Properties();
        try {
            Resource resource=  new ClassPathResource("foundation.properties");
            properties.load(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
            // 修改属性值
            properties.setProperty(key, value);
            // 保存修改后的properties文件
            OutputStream outputStream = new FileOutputStream(resource.getURL().getPath());
            properties.store(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void setNotInitializedFileSystem()
    {

        modifyKey("init_file_system","false");
        this.globalVariable.foundation.initFileSystem=false;
    }
}
